//
//  HandoverDeptSelectBottomView.swift
//  MedicalUnion
//
//  Created by apple on 2019/8/14.
//  Copyright © 2019 William_Xue. All rights reserved.
//

import UIKit

protocol LGFBottomPickerViewDelegate: class {
    func selectIndex(view: LGFBottomPickerView!, name: String, index: Int)
}

class LGFBottomPickerView: UIView {

    weak var delegate: LGFBottomPickerViewDelegate?
    
    var bgView: UIView!
    
    var label: UILabel!
    
    var datas: [String]!
    
    var picker: UIPickerView!
    
    var selectIndex: Int!
    
    var callBack: ((LGFBottomPickerView?, String, Int) -> Void)!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showIn(_ SV: UIView, _ datas: [String], _ selectIndex: Int, _ callBack: @escaping (LGFBottomPickerView?, String, Int) -> Void) -> Void {
        self.datas = datas
        self.selectIndex = selectIndex
        self.callBack = callBack
        SV.addSubview(self)
        lgf_FillSuperview()
        
        config()
        
        bgView.transform = CGAffineTransform.init(translationX: 0, y: UIDevice.lgf_ScreenH / 2)
        DispatchQueue.main.async {
            UIView.animate(withDuration: 0.35) {
                self.bgView.transform = CGAffineTransform.identity
                self.backgroundColor = UIColor.black.withAlphaComponent(0.2)
            }
        }
    }
    
    func config() -> Void {
        bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(UIDevice.lgf_ScreenH / 2.5)
        }
        
        let topView = UIView.init()
        bgView.addSubview(topView)
        topView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(50.lgf_H())
        }
        
        let leftButton = UIButton.init(type: .custom)
        leftButton.setTitle("取消", for: .normal)
        leftButton.titleLabel!.font = UIFont.boldSystemFont(ofSize: 16)
        leftButton.setTitleColor(UIColor.init(lgf_HexString: "0F88F2"), for: .normal)
        leftButton.addTarget(self, action: #selector(leftButtonSelect(_:)), for: .touchUpInside)
        topView.addSubview(leftButton)
        leftButton.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15.lgf_W())
            make.centerY.equalToSuperview()
        }
        
        let rightButton = UIButton.init(type: .custom)
        rightButton.setTitle("确认", for: .normal)
        rightButton.titleLabel!.font = UIFont.boldSystemFont(ofSize: 16)
        rightButton.setTitleColor(UIColor.init(lgf_HexString: "0F88F2"), for: .normal)
        rightButton.addTarget(self, action: #selector(rightButtonSelect(_:)), for: .touchUpInside)
        topView.addSubview(rightButton)
        rightButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15.lgf_W())
            make.centerY.equalToSuperview()
        }
        
        label = UILabel.init()
        label.textColor = UIColor.init(lgf_HexString: "111111")
        label.font = UIFont.systemFont(ofSize: 16)
        label.textAlignment = .center
        label.text = lgf_ViewName
        topView.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.6)
        }
        
        picker = UIPickerView.init()
        picker.delegate = self
        picker.dataSource = self
        bgView.addSubview(picker)
        picker.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(topView.snp.bottom)
        }
        picker.selectRow(selectIndex, inComponent: 0, animated: false)
    }
    
    
    
    @objc func leftButtonSelect(_ sender: UIButton) -> Void {
        remove()
    }
    
    @objc func rightButtonSelect(_ sender: UIButton) -> Void {
        self.delegate?.selectIndex(view: self, name:self.lgf_ViewName ?? "", index: self.selectIndex)
        self.callBack(self, self.lgf_ViewName ?? "", self.selectIndex)
        remove()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first?.view == self {
            remove()
        }
    }
    
    func remove() -> Void {
        UIView.animate(withDuration: 0.35, animations: {
            self.bgView.transform = CGAffineTransform.init(translationX: 0, y: UIDevice.lgf_ScreenH / 2)
            self.backgroundColor = UIColor.black.withAlphaComponent(0.0)
        }) { (finish) in
            self.removeFromSuperview()
        }
    }

}

extension LGFBottomPickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.datas.count
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let title: String = self.datas![row]
        return title.components(separatedBy: "&&").first
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.selectIndex = row
    }
}
